package ma.ensao.evalmetrics.view;

import gr.uom.java.pattern.gui.Console;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

import ma.ensao.evalmetrics.controller.FileManager;
import ma.ensao.evalmetrics.model.MyFile;

import org.jfree.chart.JFreeChart;

import com.opensymphony.xwork2.ActionSupport;

public class Pattern extends ActionSupport{


	private static final long serialVersionUID = 1L;

	public Long id;
	private int param;
	private FileManager projet;
	private MyFile myFile;
	private InputStream fileInputStream;
	
	public Pattern(){
		projet = new FileManager();
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String execute(){
		myFile = projet.testerprojet(getId());
		URL r = this.getClass().getResource("/");

		// path decoded "/C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
		String decoded = "";
		try {
			decoded = URLDecoder.decode(r.getFile(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (decoded.startsWith("/")) {
		    // path "C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
		    decoded = decoded.replaceFirst("/", "");
		}
		
		for(int i=0,nb=0;i<2;i++){
			nb=0;
			if(decoded.endsWith("/")){
				decoded=decoded.substring(0,decoded.length()-1);
			}
			nb= decoded.lastIndexOf('/');
			decoded=decoded.substring(0,nb);
		}
		

		File f = new File(decoded+"\\skin\\designtree.xml");
		new Console( new File(myFile.getFolder()),f);
		
		return SUCCESS;
		
	}
	
	public String exportXML() throws Exception {
		myFile = projet.testerprojet(getId());
		File f = new File("patternsXml.xml");
		new Console( new File(myFile.getFolder()),f);
	    fileInputStream = new FileInputStream(f);
	    
	    return SUCCESS;
	}
	
	public int getParam() {
		return param;
	}
	public void setParam(int param) {
		this.param = param;
	}

	public InputStream getFileInputStream() {
		return fileInputStream;
	}
	

}
